package com.jd.org.service.impl;


import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.org.domain.Department;
import com.jd.org.mapper.DepartmentMapper;
import com.jd.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    @Transactional
    public void save(Department department) {
        //处理dirPath
        //1.没有选上级部门
        //Department( manager_id=327, manager=null, parent_id=null, parent=null, children=[])
        //2.选了上级部门
        //Department( manager_id=327, manager=null, parent_id=4, parent=null, children=[])
        //3.选了之后叉掉
        //Department( manager_id=326, manager=null, parent_id=null, parent=null, children=[])

        //添加之后会返回一个自增长id
        departmentMapper.save(department);

        //添加的时候对dirPath的处理
        String dirPath = "";
        if (department.getParent_id()== null){//没有选择上级部门 or //选了之后×掉了
            Long id = department.getId();
            dirPath = "/" + id;
        } else {
            //获取上级部门
            Long parent_id = department.getParent_id();
            //获取上级部门的dirPath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }
            department.setDirPath(dirPath);
            departmentMapper.update(department);

    }




    @Override
    @Transactional
    public void update(Department department) {
        //添加的时候对dirPath的处理
        String dirPath = "";
        if (department.getParent_id()== null){//没有选择上级部门 or //选了之后×掉了
            Long id = department.getId();
            dirPath = "/" + id;
        } else {
            //获取上级部门
            Long parent_id = department.getParent_id();
            //获取上级部门的dirPath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }



    @Override
    public List<Department> deptTree() {
        //1.一次性查找所有数据,放到内存中,可以使用map
        //2.从所有数据中找到第一层级部门/顶级部门:这就是我们要返回的数据
        //3.遍历所有部门,只要不是第一层级的,找自己的上级,并把自己放入上级的children中
        //将所有的部门装入map中 - 每次获取父部门 都从这里获取
        List<Department> all = departmentMapper.loadAll();
        Map<Long, Department> map = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(),department);
        }

        //前端需要得到的集合数据 - 部门树

        List<Department> deptTree = new ArrayList<>();
        for (Department department : all) {
            //通过parent_id是否存在,判断该部门是否有上级部门
            if (department.getParent_id() == null){
                //如果是顶级部门,则将其添加进部门树中
                deptTree.add(department);
            } else {
                //1.通过parent_id判断,该部门有上级部门
                Long parent_id = department.getParent_id();
                //通过parent_id从map中获取上级部门
                //下面这种是从Map中去获取的 - 以前装进去的对象还是会存在
                Department parentDepartment = map.get(parent_id);
                //2.将自己装入父部门的 Children中
                parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
